package org.brdevils.bf2sas.client;

import java.util.Iterator;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Main implements EntryPoint {
    private TextBox input = new TextBox();
    private VerticalPanel mainPanel = new VerticalPanel();
    private Button sendButton = new Button("send");
    private Button connectButton = new Button("connect");
    private TextArea response = new TextArea();
    private TelnetServiceAsync telnetService;

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
        telnetService = (TelnetServiceAsync) GWT.create(TelnetService.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) telnetService;
        endpoint.setServiceEntryPoint(GWT.getModuleBaseURL() + "telnet");
        
        telnetService.receive(new ReceiveCallback());

        mainPanel.setWidth("100%");
        mainPanel.setHeight("100%");
        input.setWidth("100%");
        response.setWidth("100%");
        response.setVisibleLines(20);
        response.setEnabled(false);

        mainPanel.add(connectButton);
        mainPanel.add(input);
        mainPanel.add(sendButton);
        mainPanel.add(response);

        connectButton.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                telnetService.connect("pass", new AsyncCallback() {
                    public void onFailure(Throwable caught) {
                        GWT.log("erro conectando", caught);
                    }

                    public void onSuccess(Object result) {
                        handleEvents((List) result);
                        telnetService.receive(new ReceiveCallback());
                    }
                });
            }
        });
        sendButton.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                telnetService.send(input.getText(), new AsyncCallback() {
                    public void onFailure(Throwable caught) {
                        GWT.log("erro conectando", caught);
                    }

                    public void onSuccess(Object result) {
                    }
                });
            }
        });

        RootPanel.get().add(mainPanel);
    }

    private void handleEvents(List events) {
        if (events != null && events.size() > 0) {
            Iterator i = events.iterator();
            while (i.hasNext()) {
                Event event = (Event) i.next();
                if (event instanceof ResponseEvent) {
                    ResponseEvent respEvent = (ResponseEvent) event;
                    String text = response.getText();
                    text = text + "\n" + respEvent.getReponse();
                    response.setText(text);
                }
            }
        }
    }
    
    private class ReceiveCallback implements AsyncCallback {

        public void onFailure(Throwable caught) {
            GWT.log("erro recebendo eventos: " + caught.getMessage(), caught);
        }

        public void onSuccess(Object result) {
            List events = (List) result;
            handleEvents(events);
            telnetService.receive(new ReceiveCallback());
        }
    }
}
